package com.pri.chat.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.LookMemberListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.pri.chat.R;
import com.pri.chat.adapter.LookMemberAdapter;
import com.pri.chat.adapter.ShareMemberAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.nim.session.extension.ShareAttachment;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class ShareMemberActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;

    @Override
    protected int getLayoutId() {
        return R.layout.activity__share_member;
    }

    private String id;
    private String image;
    private String title;
    private String content;
    private int type;
    @Override
    protected void initView() {
        id = getIntent().getExtras().getString("id");
        image = getIntent().getExtras().getString("image");
        title = getIntent().getExtras().getString("title");
        content = getIntent().getExtras().getString("content");
        type = getIntent().getExtras().getInt("type");

        setTitle("选择用户");
        srlAll.setEnableLoadmore(false);
        initLayout();
        initSrl();
    }

    private List<LookMemberListBean> mList;
    private ShareMemberAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = false;

    private void initLayout() {
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new ShareMemberAdapter(R.layout.item_look_member, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<LookMemberListBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
            } else {
                none.setVisibility(View.INVISIBLE);
                mList.addAll(baseBean.getData());
                mAdapter.notifyDataSetChanged();
            }
        };
        Map<String,Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId",SharedHelper.readId(this));
        dtModel.put("pageIndex",page);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().getShareMember(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            initData();
        });
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ShareAttachment attachment = new ShareAttachment();
        attachment.setDescribe(content);
        attachment.setTitle(title);
        attachment.setImageUrl(image);
        attachment.setId(id);
        attachment.setType(type);
        IMMessage message = MessageBuilder.createCustomMessage(mList.get(position).getId(), SessionTypeEnum.P2P, attachment);
        NIMClient.getService(MsgService.class).sendMessage(message, false);
        finish();
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//        Bundle bundle =new Bundle();
//        bundle.putString("id",mList.get(position).getId());
//        RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class,bundle);
    }
}
